using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_HO_ManagementBranch : System.Web.UI.Page
{
    func fn = new func();
    string idb = null;
    protected void Page_Load(object sender, EventArgs e)
    {//kiem tra request act
        if (Request.QueryString["act"] != null) {
            
            switch (Request.QueryString["act"].ToString()) { 
                case"del":
                    
                    if(Request.QueryString["idb"]!=null){
                       
                        idb = Request.QueryString["idb"].ToString();
                        if (fn.BranchCheckExistByID(idb) == true)
                        {
                            string sql ="delete  BranchesOnRoute where BranchID='" + idb + "';"+ "delete from Branches where BranchID='" + idb + "'";
                            if (fn.EXECQUERY(sql) > 0)
                            {
                                lblthongbao.Text = "Deleted";
                            }
                            else {

                                lblthongbao.Text = "Cannot delete branches";
                            }
                            

                        }
                        else {
                            lblthongbao.Text = "Branch ID not exist!";
                        }
                    }
                    break;
                case "changestatus":

                    if (Request.QueryString["idb"] != null)
                    {

                          idb = Request.QueryString["idb"].ToString();
                          if (fn.BranchCheckExistByID(idb) == true)
                        {
                            string status = "1";
                            if (Request.QueryString["status"] != null)
                            {
                                if (Request.QueryString["status"].ToString() == "1")
                                {
                                    status = "0";
                                }
                            }
                            string sql = "update branches set Status='"+status+"' where BranchID='"+idb+"'";
                            int i=fn.EXECQUERY(sql);
                            if (i > 0) {
                                lblthongbao.Text = "Change status successfully ";
                            }

                        }
                        else
                        {
                            lblthongbao.Text = "Branch ID not exist!";
                        }
                    }
                    break;
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {//mo panel create
        pnCreate.Visible = true;
        //hide nut update
        btnUpdate.Visible = false;
        pnList.Visible = false;
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {//doi ten form
        lblTitleForm.Text = "Edit branches ";
        pnCreate.Visible = true;
        btnUpdate.Visible = true;
        btnCreate.Visible = false;
        pnList.Visible = false;
        
        if (IsPostBack)
        {//load info
            string idb = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            DataRow dr = fn.GETDATAROWS("select * from branches where BranchID='" + idb + "'");
            lblID.Text = dr["BranchID"].ToString();
            txtName.Text = dr["BranchName"].ToString();
            txtPhone.Text = dr["Phone"].ToString();
            txtLocation.Text = dr["Location"].ToString();
        }
        


    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (fn.BranchCheckExistByID(lblID.Text) == true)
        {

           string sql = "update branches set BranchName='" + txtName.Text + "',Location='" + txtLocation.Text + "',Phone='" + txtPhone.Text + "' where BranchID='" + lblID.Text + "';";
           if (fn.EXECQUERY(sql) > 0)
           {
               lblthongbao.Text = "Updated!";
           }
           else {
               lblthongbao.Text = "Update false!";
           }

            

        }
        else {
            lblthongbao.Text = "Update false!";
        }
        
            
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (fn.BranchCreate(txtName.Text, txtLocation.Text, txtPhone.Text) == true)
        {
            lblthongbao.Text = "Created!";
        }
        else {
            lblthongbao.Text = "Cannot create!";
        }
        
    }
}
